#!/system/bin/sh

[ -e $TMPDIR/*Beta* ] && set -x

DXH_ID="$$"
DXH_Id="${0##*/}"
DXH_Dir="${0%/*}"

DXH_V() {

echo "- 作者：酷安@董小豪"
echo "- 脚本已有最新版本(2020051807)，当前为旧版版(2020041914)。"

}

DXH_H() {
echo "- 作者：酷安@董小豪"
echo "- 脚本已有最新版本(2020051807)，当前为旧版版(2020041914)。"
}

download() {

Download_Dir=$1
Download_Name=$2
Download_ID="icswbgj"
Download_DaXiao="3615466"
Download_MD5="3f5164d08d5b55f97f7dec9dd94ca5ec"
Download_DIR=$Download_Dir/$Download_Name

Download_DaXiao_2=`echo -n $Download_DaXiao | wc -c`
[[ $Download_DaXiao_2 -ge 10 ]] && Download_MD5=$4 && Download_DaXiao=$5

if [ -f $TMPDIR/*/curl ]; then
	curl=$TMPDIR/*/curl
else
	curl=$curl
fi

if [[ -z $Download_Dir && -z $Download_Name && -z $Download_ID ]]; then
	echo
	[[ -z $Download_Dir ]] && echo "- 路径异常！！！"
	[[ -z $Download_Name ]] && echo "- 名称异常！！！"
	[[ -z $Download_ID ]] && echo "- 下载ID异常！！！"
	exit -1
fi

if [[ -n $Download_MD5 && -f $Download_DIR ]]; then
	MD5=`md5sum $Download_DIR | sed 's/ .*//g'`
	if [[ $MD5 != $Download_MD5 ]]; then
		rm -f $Download_DIR
	else
		echo "- [$Download_Name] 文件已存在！！！"
		exit 0
	fi
fi

[[ -f $Download_DIR ]] && WenJian=`wc -c < $Download_DIR`
if [[ -n $WenJian && -n $Download_DaXiao ]]; then
	if [[ $WenJian != $Download_DaXiao ]]; then
		rm -f $Download_DIR
	else
		echo "- [$Download_Name] 文件已存在！！！"
		exit 0
	fi
fi

XiaZai() {

model=`getprop ro.product.model`
model_version=`getprop ro.build.version.release`
model=${model:=MI 8 Lite}
model_version=${model:=10}

echo -n "- 连接中❶ " && UA=https://vip.d0.baidupan.com/file/`$curl -skA "Mozilla/5.0 (Linux; Android ${model_version}; ${model}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Mobile Safari/537.36" https://www.lanzous.com/tp/$Download_ID | egrep "\"\?|'\?" | sed "s/'//g;"'s/"//g;s/.*?/?/g'`
echo "❷\n-----------------------------------------"&& $curl -skLH "Accept-Language: zh-CN,en-US;q=0.9" $UA -o $Download_DIR

}
MD5() {

if [[ -n $Download_MD5 ]]; then
	MD5=`md5sum $Download_DIR | sed 's/ .*//g'`
	if [[ $MD5 != $Download_MD5 ]]; then
		echo "MD5校验 [$Download_Name]失败！！！"
		rm -f $Download_DIR
	fi
fi

}

Han=0
XiaZai &
[[ $Download_DaXiao -le 1024 ]] && DaXiao=${Download_DaXiao}b
[[ $Download_DaXiao -ge 1024 ]] && DaXiao=`awk "BEGIN{print $Download_DaXiao/1024}"`kb
[[ $Download_DaXiao -ge 1048576 ]] && DaXiao=`awk "BEGIN{print $Download_DaXiao/1048576}"`MB
[[ $DaXiao == b ]] && DaXiao="未知"
echo "\n- 正在下载 [$Download_Name]……文件总大小：$DaXiao"
if [[ -n $Download_DaXiao ]]; then
	until [[ $Han = 1 ]]; do
		[[ -f $Download_DIR ]] && MBK=`wc -c < $Download_DIR`
		[[ -f $Download_DIR ]] && [[ $MBK -le 1024 ]] && DaXiao2=${Download_DaXiao}b
		[[ -f $Download_DIR ]] && [[ $MBK -ge 1024 ]] && DaXiao2=`awk "BEGIN{print $Download_DaXiao/1024}"`kb
		[[ -f $Download_DIR ]] && [[ $MBK -ge 1048576 ]] && DaXiao2=`awk "BEGIN{print $Download_DaXiao/1048576}"`MB
	sleep 1
		[[ -n $MBK ]] && Percentage=`awk "BEGIN{print $MBK/($Download_DaXiao/100)}"`
		[[ -n $Jin ]] && [[ -n $Percentage ]] && echo "progress:[${Percentage%.*}/100]"
		[[ -n $Jin ]] && [[ -n $MBK ]] && echo "- 已下载：$DaXiao2/$DaXiao 已完成${Percentage}%\n-----------------------------------------"
		[[ -n $MBK ]] && [[ $MBK == $Download_DaXiao ]] && let Han=1 && MD5
	done
fi

}

case $1 in

	-x|-xia|-xiazai)
		download $2 $3 $4 $5 $6
	;;

	-xj|-jx)
		let Jin=$1
		download $2 $3 $4 $5 $6
	;;

	*)
		DXH_V
		DXH_H
	;;

esac

set +x